package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOCustomers extends DataAccesObject implements IfDAO{
	
	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM customers " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOCustomers customer = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM customers " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				customer = new DTOCustomers(rs.getInt("id"), rs.getString("name"),
						rs.getString("lastName"), rs.getString("email"), rs.getString("phone"),
						rs.getString("password"), rs.getString("userName"), rs.getBoolean("available"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customer;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOCustomers customer = (DTOCustomers) bean;
		Connection con = getConexion();
		String query = "INSERT INTO customers VALUES( DEFAULT"+
						", '"+customer.getName() + "'" +
						", '"+customer.getLastName() + "'" +
						", '"+customer.getEmail() + "'" +
						", '"+customer.getPhone() + "'" +
						", '"+customer.getPassword() + "'" +
						", '"+customer.getUserName() + "'" +
						", '"+ (customer.isAvailable()? 1:0) + "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> customers = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM customers " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOCustomers customer = new DTOCustomers(rs.getInt("id"), rs.getString("name"),
						rs.getString("lastName"), rs.getString("email"), rs.getString("phone"),
						rs.getString("password"), rs.getString("userName"), rs.getBoolean("available"));
				customers.add(customer);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customers;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOCustomers customer = (DTOCustomers) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE customers SET" +
						" name='" + customer.getName() + "'" +
						" ,lastName='" + customer.getLastName() + "'" +
						" ,email='" + customer.getEmail() + "'" +
						" ,phone='" + customer.getPhone() + "'" +
						" ,password='" + customer.getPassword() + "'" +
						" ,userName='" + customer.getUserName() + "'" +
						" ,available='" + (customer.isAvailable()? 1:0) + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}
}
